package com.ebt.m.homepage;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ebt.m.R;

/**
 * Created by Andy.Wang on 2016/4/14.
 */
public class ActionItemView extends RelativeLayout {
    private int drawableRes = R.drawable.ic_avatar;
    private String actionName;
    private ImageView mActionImg;
    private TextView mActionName;
    private MARK_MODE mCurMark_mode = MARK_MODE.NONE;
    private TextView mCircleNum;
    private ImageView mCirclePos;
    private int mTextColor;
    private int mNum;

    private enum MARK_MODE {
        NUMBER,
        POS,
        NONE
    }

    public ActionItemView(Context context) {
        this(context, null);
    }

    public ActionItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ActionItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View.inflate(context, R.layout.layout_action_item, this);
        mActionImg = (ImageView) findViewById(R.id.action_image);
        mActionName = (TextView) findViewById(R.id.action_name);
        mCircleNum = (TextView) findViewById(R.id.circle_num);
        mCirclePos = (ImageView) findViewById(R.id.circle_pos);
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.ActionItemView, defStyleAttr, 0);
        drawableRes = attributes.getResourceId(R.styleable.ActionItemView_action_img, R.drawable.ic_avatar);
        actionName = attributes.getString(R.styleable.ActionItemView_action_name);
        mNum = attributes.getInteger(R.styleable.ActionItemView_action_num, 0);
        mTextColor = attributes.getColor(R.styleable.ActionItemView_text_color, Color.parseColor("#333333"));
        attributes.recycle();

        mActionImg.setBackgroundResource(drawableRes);
        mActionName.setText(actionName);
        mActionName.setTextColor(mTextColor);
        mActionImg.post(new Runnable() {
            @Override
            public void run() {
                //setNum(mNum);
            }
        });
    }

    public void setNum(int num) {
        if (num == 0) {
            this.mCurMark_mode = MARK_MODE.NONE;
            mCircleNum.setVisibility(View.GONE);
            mCirclePos.setVisibility(View.GONE);
        } else {
            mCurMark_mode = MARK_MODE.NUMBER;
            mCircleNum.setVisibility(VISIBLE);
            mCirclePos.setVisibility(GONE);
            mCircleNum.setText(num + "");
        }

    }

    public void setPos() {
        this.mCurMark_mode = MARK_MODE.POS;
        mCircleNum.setVisibility(View.GONE);
        mCirclePos.setVisibility(View.VISIBLE);
    }

}
